#coding=utf-8
from method.driver import Proxy
import random  # 取随机数
from config.conf import proxiesDEF
from music.play import playMusic # pip install pygame
import time
from config.conf import proxiesDEF, Referer_list
from method.readCheckAsin import readCheckAsinTxt
from method.readWhiteNameTxt import readWhiteNameTxt
from method.readInterimWhiteNameTxt import readInterimWhiteNameTxt
from method.readPhoneTxt import readPhoneTxt
from method.sendEmail import SendEmail
from method.callPhone import SendCodeViewSet


get_goods_num = 1
goodsArr = []
Referer_list_len = len(Referer_list) - 1
whiteNameArr = readWhiteNameTxt()
checkAsinArr = readCheckAsinTxt()
InterimWhiteNameArr = readInterimWhiteNameTxt()
phoneNumberArr = readPhoneTxt()
whiteNameArr.extend(InterimWhiteNameArr)
print(phoneNumberArr)
def getAsin():
    proxies_list = ['221.122.91.60:80', '125.123.127.251:9000', '125.73.220.18:49128', '106.12.201.174:8080', '125.123.124.192:9000', '125.123.126.137:9000', '125.123.65.5:9000', '27.195.216.21:8118', '222.128.9.235:33428', '125.123.121.214:9000', '114.249.113.151:9000', '123.56.74.221:80', '223.156.112.100:9000', '47.104.172.108:8118', '125.123.124.145:9000', '221.122.91.65:80', '122.136.212.132:53281', '125.123.44.147:9000', '218.108.175.15:80', '52.83.202.3:8118', '47.107.158.219:8000', '124.205.155.157:9090', '182.35.81.10:9999', '45.125.32.181:3128', '182.35.86.175:9999', '1.198.72.162:9999', '163.204.242.30:9999', '125.123.44.81:9000', '61.160.210.223:808', '119.178.101.18:8888', '221.122.91.61:80', '123.132.232.254:61017', '114.225.29.112:8118', '43.254.168.56:53281', '69.160.173.3:8000', '123.163.97.154:9999', '163.204.244.166:9999', '163.204.243.219:9999', '163.204.245.109:9999', '125.123.66.161:9000', '134.175.55.15:8888', '182.35.80.251:9999', '125.123.141.95:9000', '163.125.70.239:8888', '218.60.8.83:3129', '125.65.79.60:3311', '116.52.100.233:8888', '182.122.205.72:8118', '106.9.169.41:808', '175.43.34.17:9999']

    # boo = [True, False, True, False, False, False, False, False, False]
    # boo = [False]
    # try:
    #     if random.choice(boo):
    #         proxies_list = proxiesDEF()
    #         print(proxies_list)
    # except:
    #     print('获取ip失败')
    ip = proxies_list[random.randint(0, len(proxies_list)-1)]
    driver = Proxy(ip).getdriver()

    if len(checkAsinArr) > 0:
        checkfollowSeller(driver, checkAsinArr)
    else:
        driver.quit()
        print('=========请设置要检查的 asin ==============')


def checkfollowSeller(driver, checkAsinArr):
    global get_goods_num
    asin = checkAsinArr[0]
    print('==%s========================%s===============' % (asin, get_goods_num))
    url = 'https://www.amazon.com/gp/offer-listing/%s' % asin
    driver.get(url)
    driver.implicitly_wait(random.uniform(2.5, 4.3))
    followArr = []
    items = driver.find_elements_by_css_selector('#olpOfferList .a-row.a-spacing-mini.olpOffer')
    for index, item in enumerate(items):
        sellerID = items[index].find_element_by_xpath('.//div[4]/h3/span/a').get_attribute('href').strip().split('seller=')[1]
        followArr.append(sellerID)

    for follow in followArr:
        isWhite = False
        # 我们设置的 whiteNameArr 白名单商家
        for white in whiteNameArr:
            if follow == white:
                isWhite = True
        if not isWhite:
            print('你的 %s 被 ====>>   %s  跟卖啦!!!' % (asin, follow))
            # 检测的点 asin     跟卖你的点 followAsin
            # 随机找一个电话
            phone = random.choice(phoneNumberArr)
            print(phone)
            # SendCodeViewSet().post(phone)
            # SendCodeViewSet().post('13622327097')
            SendEmail().send_email(asin, follow)
            playMusic('./music/TheCross.mp3', 1, 0.0)

        else:
            print('%s === 这个是白名单的 seller  ' % follow)
        # try:
        #     driver.get(url)
            # driver.implicitly_wait(random.uniform(2.5, 4.3))
            # name = driver.find_element_by_xpath('//*[@id="productTitle"]').text
    #
    # driver.quit()

    # .get_attribute('href')
    # driver.implicitly_wait(random.uniform(13.5, 15.3))
    time.sleep(random.uniform(613.5, 615.3))
    get_goods_num = get_goods_num + 1
    checkfollowSeller(driver, checkAsinArr)
    # print(len(resList))





if __name__ == '__main__':
    getAsin()
